/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Entity.MonHocEntity;
import Util.ConSQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thuyngaytho
 */
public class MonHocDao {

    ConSQL cs = new ConSQL();

    //Them
    public ArrayList<MonHocEntity> getAll() {
        MonHocEntity mhEntity;
        ArrayList<MonHocEntity> list = new ArrayList<MonHocEntity>();
        try {
            PreparedStatement ps = cs.getCon().prepareStatement("select * from monHoc");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                mhEntity = new MonHocEntity();
                mhEntity.setMaMH(rs.getString("maMH"));
                mhEntity.setTenMH(rs.getString("tenMH"));
                mhEntity.setSoTC(rs.getInt("soTC"));
                mhEntity.setHesoCC(rs.getFloat("HSChuyenCan"));
                mhEntity.setHesoGK(rs.getFloat("HSGiuaky"));
                mhEntity.setHesoKT(rs.getFloat("HSThiKetthuc"));

                list.add(mhEntity);
            }
        } catch (Exception e) {
        }
        return list;
    }

    public ArrayList<MonHocEntity> timKiem(String str, int id) {
        MonHocEntity mhEntity;
        ArrayList<MonHocEntity> list = new ArrayList<MonHocEntity>();
        try {
            PreparedStatement ps;
            if (id == 0) {
                ps = cs.getCon().prepareStatement("select * from monHoc where maMH like N'%" + str + "%'");
            } else {
                ps = cs.getCon().prepareStatement("select * from monHoc where tenMH like N'%" + str + "%'");
            }
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                mhEntity = new MonHocEntity();
                mhEntity = new MonHocEntity();
                mhEntity.setMaMH(rs.getString("maMH"));
                mhEntity.setTenMH(rs.getString("tenMH"));
                mhEntity.setSoTC(rs.getInt("soTC"));
                mhEntity.setHesoCC(rs.getFloat("HSChuyenCan"));
                mhEntity.setHesoGK(rs.getFloat("HSGiuaky"));
                mhEntity.setHesoKT(rs.getFloat("HSThiKetthuc"));

                list.add(mhEntity);
            }
        } catch (Exception e) {
        }
        return list;
    }
}
